package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.SystemConstantDAO;
import cn.edu.nju.software.greencreditsystem.domain.SystemConstant;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-19
 * 类描述：SystemConstant类DAO实现
 */
public class SystemConstantDAOImpl implements SystemConstantDAO
{

    @Override
    public double get(String name)
    {
        Double result = null;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            SystemConstant sc = (SystemConstant) s.get(SystemConstant.class, name);
            result = sc.getValue();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean set(String name, double value)
    {
        boolean result = false;
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            SystemConstant sc = (SystemConstant) s.get(SystemConstant.class, name);
            sc.setValue(value);
            s.update(sc);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }finally
        {
            s.close();
        }
        
        return result;
    }
    
}
